<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox
        v-for=" role in list"
        :key="role.id"
        :label="role.id"
      >
        {{ role.name }}
      </el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>

  </div>
</template>

<script>
import { getRoles } from '@/api/settings'
import { getUserDetailById } from '@/api/user'// 获取用户信息
import { assignRoles } from '@/api/employees'// 调用分配员工角色接口
export default {
  props: { // 从父元素获取到点击当前分配对象的ID
    employeeId: {
      type: String,
      required: true
    }
  },

  data() {
    return {
      roleIds: [], // 当前选中角色
      list: [] // 获取的角色列表
    }
  },
  created() {
    this.loadRoles()
  },

  methods: {

    async loadRoles() {
      const res = await getRoles({ page: 1, pagesize: 100 })
      console.log('获取角色列表', res)
      this.list = res.data.rows

      const info = await getUserDetailById(this.employeeId)
      console.log('获取用户信息info', info)
      this.roleIds = info.data.roleIds
    },

    closeDialog() {
      this.$emit('close')
    },

    async hSubmit() {
      const res = await assignRoles({ id: this.employeeId, roleIds: this.roleIds })
      console.log('分配角色', res)
      this.$emit('update-close')// 分配角色成功, 关闭弹窗并更新
    }
  }
}
</script>
